//
//  PywPasswordView.m
//  ANB_PasswordView_Demo
//
//  Created by smallwave on 16/7/26.
//  Copyright © 2016年 yyft. All rights reserved.
//

#import "PywPasswordView.h"

@interface PywPasswordView ()

@property (nonatomic, weak) UITextField *textField;         // 密码输入框
@property (nonatomic, strong) NSMutableArray *dataSource;   // 用于保存输入密码的数组

@end

@implementation PywPasswordView

#pragma mark - Init
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        UITextField *textField = [[UITextField alloc] initWithFrame:self.bounds];
        textField.hidden = YES;
        textField.keyboardType = UIKeyboardTypeNumberPad;
        [textField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
        [self addSubview:textField];
        self.textField = textField;
    }
    return self;
}
#pragma mark - Events
/// 文本框内容改变
- (void)textChange:(UITextField *)textField{
    NSString *password = textField.text;
    if (password.length > self.elementCount) {
        return;
    }
    
    for (int i = 0; i < _dataSource.count; i++) {
        UITextField *pwdTextField = [_dataSource objectAtIndex:i];
        if (i < password.length) {
            NSString *pwd = [password substringWithRange:NSMakeRange(i, 1)];
            pwdTextField.text = pwd;
        }else{
            pwdTextField.text = nil;
        }
    }
    
    if (password.length == _dataSource.count) {
        [textField resignFirstResponder];  // 移除第一响应者，隐蔽键盘
    }
    
    !self.passwordBlock ? : self.passwordBlock(textField.text);
}
- (void)clearText{
    self.textField.text = nil;
    [self textChange:self.textField];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = (self.bounds.size.width - (self.elementCount - 1) * self.elementMargin) / self.elementCount;
    CGFloat h = self.bounds.size.height;
    for (NSUInteger i = 0; i < self.dataSource.count; i++) {
        UITextField *pwdTextField = [_dataSource objectAtIndex:i];
        x = i * (w + self.elementMargin);
        pwdTextField.frame = CGRectMake(x, y, w, h);
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.textField becomeFirstResponder];
}

#pragma mark - Getter & Setter
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray arrayWithCapacity:self.elementCount];
    }
    return _dataSource;
}
- (void)setElementCount:(NSUInteger)elementCount{
    _elementCount = elementCount;
    for (int i = 0; i < self.elementCount; i++) {
        UITextField *pwdTextField = [[UITextField alloc] init];
        pwdTextField.layer.borderColor = [UIColor grayColor].CGColor;
        pwdTextField.enabled = NO;
        pwdTextField.textAlignment = NSTextAlignmentCenter;
        pwdTextField.secureTextEntry = YES;
        pwdTextField.layer.borderWidth = 0.5;
        pwdTextField.userInteractionEnabled = YES;
        [self insertSubview:pwdTextField belowSubview:self.textField];
        [self.dataSource addObject:pwdTextField];
    }
}
- (void)setElementColor:(UIColor *)elementColor{
    _elementColor = elementColor;
    for (NSUInteger i = 0; i < self.dataSource.count; i++) {
        UITextField *pwdTextField = [_dataSource objectAtIndex:i];
        pwdTextField.layer.borderColor = self.elementColor.CGColor;
    }
}
- (void)setElementMargin:(NSUInteger)elementMargin{
    _elementMargin = elementMargin;
    [self setNeedsLayout];
}

@end
